package com.alan.wifishare.view.impl.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

import com.alan.libapi.api.ALTcpClient;
import com.alan.libapi.api.ALTcpRecvNotifyClient;
import com.alan.libapi.api.RecvNotifyListener;
import com.alan.libapi.business.dao.BusinessTcpImpl;
import com.alan.libapi.model.business.DeviceStatisticsInfo;
import com.alan.libapi.model.business.TcpJsonResponseOfStatus;
import com.alan.libapi.model.business.TcpRecvNotifyDeviceState;
import com.alan.libapi.model.business.TcpResponseStatus;
import com.alan.library.allog.KLog;
import com.alan.wifishare.app.GlobalField;
import com.alibaba.fastjson.JSON;

/**
 * @className: BusinessService
 * @describtion: BusinessService
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/5/3 14:58
 */
public class BusinessService extends Service {
    public static String ACTION_START_SERVICE = "action_start_service";
    public static String ACTION_STOP_SERVICE = "action_stop_service";
    public static final String socketIP = "192.168.1.10";
    public static final int socketPort = 12346;
    public static final int RECV_NOTIFY_SOCKET_PORT = 13427;

    /**
     * static method to start service
     * @param context
     */
    public static void startService(Context context) {
        KLog.i("startService(Context context)");
        Intent intent = new Intent(context, BusinessService.class);
        intent.setAction(ACTION_START_SERVICE);
        context.startService(intent);
    }

    /**
     * static method to stop service
     * @param context
     */
    public static void stopService(Context context) {
         KLog.i("stopService");
        Intent intent = new Intent(context, BusinessService.class);
        intent.setAction(ACTION_STOP_SERVICE);
        context.startService(intent);
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
         KLog.i("intent.getAction()="+intent.getAction());
        // option based on action
        if (intent.getAction().equals(ACTION_START_SERVICE) == true) {
            //startService();do on {@link #onCreat()}
        } else if (intent.getAction().equals(ACTION_STOP_SERVICE) == true) {
            stopSelf();
        }
        //重传Intent。使用这个返回值时，如果在执行完onStartCommand后，服务被异常kill掉，系统会自动重启该服务，并将Intent的值传入。
        return Service.START_REDELIVER_INTENT;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        startToConnectDeviceServer();
    }

    private void startToConnectDeviceServer(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                ALTcpClient alTcpClient = new ALTcpClient(socketIP, socketPort);
                GlobalField.businessTcp.setALTcpClient(alTcpClient);
//                GlobalField.alTcpRecvNotifyClient = new ALTcpRecvNotifyClient(socketIP, RECV_NOTIFY_SOCKET_PORT);
//                GlobalField.alTcpRecvNotifyClient.setRecvNotifyListener(new RecvNotifyListener() {
//                    @Override
//                    public void onNotifyReceivecd(int requestCode, String requestString) {
//                        if(requestCode == 1){
//                            TcpRecvNotifyDeviceState tcpRecvNotifyDeviceState = JSON.parseObject(requestString, TcpRecvNotifyDeviceState.class);
//                            DeviceStatisticsInfo deviceStatisticsInfo = tcpRecvNotifyDeviceState.getParams();
//                            KLog.e(deviceStatisticsInfo.toString());
//                            TcpJsonResponseOfStatus tcpJsonResponseOfStatus = new TcpJsonResponseOfStatus(requestCode, 1);
//
//                            GlobalField.alTcpRecvNotifyClient.responseClient(JSON.toJSONString(tcpJsonResponseOfStatus));
//                        }
//                    }
//                });
            }
        }).start();
    }


    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}
